package com.txsj.scan.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.greendao.GoodsInfoDao;
import com.txsj.scan.R;
import com.txsj.scan.activity.LabelActivity;
import com.txsj.scan.adapter.HistoryAdapter;
import com.txsj.scan.base.BaseFragment;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.base.IView;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.db.GreenDaoUtils;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.widget.swipemenu.SwipeRecyView;

import java.util.List;

/**
 * @Created by WangChuangJun on 2019/2/25
 * @Describe
 */
public class HistoryGoodsFragment extends BaseFragment<BasePresenter> implements IView {

    SwipeRecyView recyclerView;
    XRefreshView xRefreshView;
    HistoryAdapter adapter;

    @Override
    protected int layoutId() {
        return R.layout.fragment_history;
    }

    @Override
    public void init() {
        xRefreshView = (XRefreshView) rootView.findViewById(R.id.xRefreshView);
        initEmptyView();
        recyclerView = (SwipeRecyView)rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        adapter =new HistoryAdapter(activity);
        recyclerView.setAdapter(adapter);
        xRefreshView.enableEmptyView(false);
        xRefreshView.setAutoRefresh(false);
        xRefreshView.setPullRefreshEnable(false);

        List<GoodsInfo> list = GreenDaoUtils.getSingleTon().getmDaoSession().getGoodsInfoDao().queryBuilder()
                .where(GoodsInfoDao.Properties.Bind_memory.eq(1))
                .list();
        if (null != list && list.size() > 0){
            adapter.setList(list);
            xRefreshView.enableEmptyView(false);
        }else {
            xRefreshView.enableEmptyView(true);
        }
        adapter.setOnItemClickListener(new HistoryAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                GoodsInfo goodsInfo = adapter.get(position);
                Intent intent = activity.getIntent();
                intent.putExtra("data", new Gson().toJson(goodsInfo));
                activity.setResult(LabelActivity.RESULT_GOODS_CODE,intent);
                activity.finish();
            }
        });
    }

    @Override
    protected BasePresenter providePresenter() {
        return new BasePresenter(this) {
            @Override
            protected void initModel() {

            }
        };
    }

    @Override
    public Context context() {
        return activity;
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(activity, R.layout.view_empty, null);
        emptyView.setBackgroundColor(getResources().getColor(R.color.white));
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });

    }
}
